package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.EmployeeDto;
import com.yc.sole.erp.entity.Employee;
import com.yc.sole.erp.mapper.EmployeeMapper;
import com.yc.sole.erp.request.EmployeeQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-04
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeRepository extends ServiceImpl<EmployeeMapper, Employee> {

    /**
     * 新增
     *
     * @param employeeDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveEmployee(EmployeeDto employeeDto) {
        Employee entity = new Employee();
        BeanUtil.copyProperties(employeeDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param employeeDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateEmployeeById(EmployeeDto employeeDto) {
        Employee entity = new Employee();
        BeanUtil.copyProperties(employeeDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmployeeByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<EmployeeDto> selectEmployeeByIds(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), EmployeeDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public EmployeeDto selectEmployeeById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), EmployeeDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<EmployeeDto> selectEmployeePage(Paging page, EmployeeQueryReq queryReq) {
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.<Employee>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(queryReq.getEmployeeName()), Employee::getEmployeeName, queryReq.getEmployeeName())
                .like(CharSequenceUtil.isNotBlank(queryReq.getPhoneNum()), Employee::getPhoneNum, queryReq.getPhoneNum())
                .like(CharSequenceUtil.isNotBlank(queryReq.getIdNo()), Employee::getIdNo, queryReq.getIdNo())
                .eq(Objects.nonNull(queryReq.getSex()), Employee::getSex, queryReq.getSex())
                .eq(Objects.nonNull(queryReq.getPositionState()), Employee::getPositionState, queryReq.getPositionState())
                .in(CollUtil.isNotEmpty(queryReq.getPositionStateList()), Employee::getPositionState, queryReq.getPositionStateList())
                .orderByAsc(Employee::getId)
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()),
                        w -> w.like(Employee::getEmployeeNo, queryReq.getKeyword())
                                .or().like(Employee::getEmployeeName, queryReq.getKeyword())
                                .or().like(Employee::getIdNo, queryReq.getKeyword())
                                .or().like(Employee::getPhoneNum, queryReq.getKeyword())
                );
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), EmployeeDto.class);
    }

    /**
     * 根据员工名称查询
     *
     * @param employeeName
     * @return
     */
    public List<EmployeeDto> selectEmployeeByName(String employeeName) {
        LambdaQueryWrapper<Employee> wrapper = Wrappers.<Employee>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(employeeName), Employee::getEmployeeName, employeeName);
        Page<Employee> employeePage = new Page<>(1, 10);
        return BeanUtil.copyToList(this.page(employeePage, wrapper).getRecords(), EmployeeDto.class);
    }
}
